import { toListData, toMapData, toTreeData } from '@/util/data'
import { enUpperCamelCase } from '@/util/index'
import { getOptionData } from '@api/option'

// list
// map
// tree
// array
export default {
  namespaced: true,
  state: () => ({
    // pageSizeList 分页 页数
    PageSizeList: [],
    // langs 多语言
    Langs: [],
    // asset_position:设备位置
    AssetPosition: [],
    // asset_type_all:设备类型
    AssetTypeAll: [],
    // asset_model_all:设备型号
    AssetModelAll: [],
    // asset_status:运行状态
    AssetStatus: [],
    // customer:供应商
    Customer: [],
    // level:重要级别
    Level: [],
    // asset_info_status:资产状态
    AssetInfoStatus: [],
    //warranties_type:保固状态
    WarrantiesType: [],
    // manufacturer:制造商
    Manufacturer: [],
    // currency
    Currency: [],
    //性别
    //key:gender_tag
    GenderTag: [],
    //启用禁用
    //is_enable
    IsEnable: [],
    //设备类型 种类
    //asset_category_type
    AssetCategoryType: [],
    //厂商
    //customerFacilities
    CustomerFacilities: [],
    // 文件类型
    // file_category
    FileCategory: [],
    //业务配置 维护配置 菜单
    //bussiness_config_type
    BussinessConfigType: [],
    //business_type 业务类型下拉框
    BusinessType: [],
    //log_whether  是否  1是2否
    LogWhether: [],
    //asset_position_permission设备位置带权限
    AssetPositionPermission: [],
    // 厂商管理 - 类型
    OrgType: [],
    // 所有厂商 all_facilities
    AllFacilities: [],
    //备件类型bom_type
    BomType: [],
    // relation_type ：适用对象，
    RelationType: [],
    // operation_permission ：操作权限 ，
    OperationPermission: [],
    // block ：所在区块
    Block: [],
    // is_show 是否显示
    IsShow: [],
    // work_column 控件类型
    WorkColumn: [],
    // multilingual 字段多语言
    Multilingual: [],
    // interface_address 工单数据,接口
    InterfaceAddress: [],
    // source_mode 来源方式
    SourceMode: [],
    // constraint_condition 条件
    ConstraintCondition: [],
    //role_list 处理人选择角色
    RoleList: [],
    // node_handle_type 处理人类型
    NodeHandleType: [],
    // work_type_all 工单类型
    WorkTypeAll: [],
    // work_template 工单模版
    WorkTemplate: []
  }),
  getters: {
    list: (state, _, rootState, rootGetters) => selectKey => {
      const key = enUpperCamelCase(selectKey)
      const map = item => {
        const { label, langtype, value } = item || {}
        return {
          label: langtype === 2 ? rootGetters['lang/get'](label) : label,
          value
        }
      }
      return toListData(state[key], map)
    },
    tree: (state, _, __, rootGetters) => selectKey => {
      const key = enUpperCamelCase(selectKey)
      const reduce = (list, item) => {
        const { label, langtype, ...data } = item || {}
        data.label = langtype === 2 ? rootGetters['lang/get'](label) : label
        return list.push(data), list
      }
      return toTreeData(state[key], reduce)
    },
    map: (state, _, __, rootGetters) => selectKey => {
      const key = enUpperCamelCase(selectKey)
      const reduce = (obj, item) => {
        const { value, label, langtype } = item || {}
        obj[value] = langtype === 2 ? rootGetters['lang/get'](label) : label
        return obj
      }
      return toMapData(state[key], reduce)
    }
  },
  mutations: {
    set(state, { key, data }) {
      state[key] = data
    }
  },
  actions: {
    async set({ commit }, { key: selectKey, out = false, option }) {
      const key = enUpperCamelCase(selectKey)
      if (key) {
        const { data } = await getOptionData({ selectKey, ...(option || {}) })
        if (data) {
          if (out) return data
          commit('set', { key, data })
        }
      }
    }
  }
}
